<?php

include_once ('../../utilities.php')

?>
<!DOCTYPE html>
<html>
<head>
	<link rel="stylesheet" type="text/css" href="../../reset.css">
	<link rel="stylesheet" type="text/css" href="../../mainStyle.css">
	<link rel="stylesheet" type="text/css" href="../../searchButton.css">
	<title>SHPE</title>
</head>
<body>

	<div id="container">
		<?php

$root = '../../';
include_once ('../../header.php')

?>
        
		<div id="page-body">
			<?php

if (!empty($_POST['chaptername']) && !empty($_POST['description']) && !empty($_POST['region']))
{
    //Used form, make data ready for sql
    $chaptername = mysqli_real_escape_string($mysqli, $_POST['chaptername']);
    $description = mysqli_real_escape_string($mysqli, $_POST['description']);
    $name = mysqli_real_escape_string($mysqli, $_POST['name']);
    $region = $_POST['region'];

    $query = "SELECT * FROM Chapters WHERE ChapterName = '" . $chaptername . "'";
    $data = $mysqli->query($query);

    if ($data->num_rows != 0)
    {
        echo "<p>Sorry, a chapter with that name is already registered. Please <a href=\"./\">click here to try again</a>.</p>";
    } else
    {
        //Insert notification to admin
        $query = "INSERT INTO Notifications (ChapterName,RegionID,Description,PresidentName) VALUES('" .
            $chaptername . "', '" . $region . "', '" . $description . "', '" . $name . "')";
        $registerquery = $mysqli->query($query);
        echo "<p> A notification has been sent to the master administrator. You may register for this chapter once the administrator verifies it. </p>";
    }
} else
{

?>
					
				   <h1>Register a Chapter</h1>
					
				   <p>Please enter the chapter details below to register.</p>
					
					<form method="post" action="./" name="registerform" id="registerform">
						<label for="chaptername">ChapterName:</label><input type="text" name="chaptername" id="chaptername" /><br />
						<label for="description">Description:</label><input type="text" size="30" maxlength="32768" name="description" id="description" /><br />
						<label for="name">President's Name:</label><input type="text" maxlength="256" name="name" id="name" /><br />
						<select name="region">
						<option value=1> 1 </option>
						<option value=2> 2 </option>
						<option value=3> 3 </option>
						<option value=4> 4 </option>
						<option value=5> 5 </option>
						<option value=6> 6 </option>
						<option value=7> 7 </option>
						</select>
						<br />
						<input type="submit" name="register" id="register" value="Register" />
					</form>
					
					<?php

}

?>
		</div>
		
		<?php

include_once ('../../footer.php')

?>
	</div>

</body>
</html>